<?php

use think\console\command\make\Model;

namespace app\index\model;
use think\Db;

use think\Model;


class UserModel extends Model
{
  protected $table = 'gjc_user';
  function topUp($uid, $num)
  {
    // return [$uid , $num];
    $money = $this->where([
      'uid' => $uid
    ])->find()['money'];
   return $this->where(['uid' => $uid])->update([
      'money' => $num + $money
    ]);
  }
  function get_order($uid){
    // return ['aaa'=>'bbb'];
    // return $uid;
    return $this->field('i.`name`, i.price , g.g_picture ,u.nickname , o.state ,o.time , o.order_id')
    ->alias('u')
    ->join('gjc_order o','o.uid = u.uid')
    ->join('gjc_goods g','o.gid = g.gid')
    ->join('gjc_goodsitem i','o.g_item_id = i.id')
    ->where(['u.uid'=>$uid])
    ->select();
  }

  //用户获得我的订单
  function get_myGoods($uid){
    


    return db('goods')
    ->where([ 'g_sponsor'=>$uid])
    ->field('gid , g_picture ,g_name ,g_start_money ,g_end_money')
    ->order('g_create_time desc')

    ->select();


  }

  // 用户获得收藏
  function getCollectGoods($uid){


    return db('change')
    ->field('g.gid , g.g_picture , g.g_name , g.g_start_money , g.g_end_money')
    ->alias('c')
    ->join('gjc_goods g','c.param1 = g.gid')
    ->order('c.time desc')
    ->where([ 
      'c.uid'=>$uid,
      'change_id'=>2000
    ])
    ->select();

  }
  
  // 用户获得聊天记录
  function userGetLog($uid){
    $list = db('chat')
    ->alias('c')
    ->field(' DISTINCT sender_id as uid , u.head ,u.nickname')
    ->join('gjc_user u','u.uid = c.sender_id','LEFT')
    ->where("rec_id ='{$uid}' or sender_id ='{$uid}'")
    ->select();
    $set = '';

    foreach ($list as $key => $value) {
      $str = $key ===0 ? $value['uid']:','.$value['uid'];
      $set.= $str;
    }

    $str = empty($set) ? "(rec_id ='{$uid}' or sender_id ='{$uid}')": "(rec_id ='{$uid}' or sender_id ='{$uid}')   and rec_id not in ({$set})";

    $list2 = db('chat')
    ->alias('c')
    ->field(' DISTINCT rec_id as uid , u.head , u.nickname')
    ->join('gjc_user u','u.uid = c.rec_id','LEFT')
    ->where($str)
    ->select();
    foreach ($list2 as $key => $value) {
      $list[]=$value;
    }


    $logList = db('chat')
    ->field('content ,rec_id ,sender_id ,time ')
    ->where("rec_id ='{$uid}' or sender_id ='{$uid}'")
    ->select();
    return [$list,$logList];

  }

  function addHistory($uid,$gid)
  {
    // 先找一下，历史记录中是否存在这条记录
    $findRes = db('change')->where(['uid'=>$uid,'param1'=>$gid,'change_id'=>2002])->find();
    if (!$findRes) {
      # code...
      $data = [
        'uid'=>$uid,
        'param1'=>$gid,
        'time'=>time(),
        'change_id'=>2002
      ];
  
      return db('change')->insert($data);
    }else{
      return db('change')->where(['id'=>$findRes['id']])->update(['time'=>time()]);
    }
  }

  function getHistory($uid){
    return db('change')
    ->field('g.gid , g.g_picture , g.g_name , g.g_start_money , g.g_end_money')
    ->alias('c')
    ->join('gjc_goods g','c.param1 = g.gid')
    ->order('c.time desc')
    ->where([ 
      'c.uid'=>$uid,
      'change_id'=>2002
    ])
    ->select();

  }

 
}
